package com.tensquare.base.controller;

import com.tensquare.base.model.Label;
import com.tensquare.base.service.LabelService;
import com.tensquare.base.util.LabelConstant;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService ;

    private Result result = new Result(true, StatusCode.OK);

    @Autowired
    private HttpServletRequest request ;

    @RequestMapping(value = "/{labelId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable("labelId")String labelId) {
        labelService.deleteById(labelId);
        result.setMessage("删除成功");
        result.setData(true);
        return result;
    }

    @RequestMapping(value = "/{labelId}",method = RequestMethod.PUT)
    public Result updateById(@PathVariable("labelId")String labelId,@RequestBody Label label) {
        label.setId(labelId);
        labelService.updateLabel(label);
        result.setMessage("更新成功");
        result.setData(true);
        return result;
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result saveLabel(@RequestBody Label label) {
        labelService.saveLabel(label);
        Label data = labelService.findById(label.getId());
        result.setMessage("保存成功");
        result.setData(data);
        return result;
    }

    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public Result searchLabel(@RequestBody Label condition){
        List<Label> labelList = labelService.searchLabel(condition);
        result.setMessage("#search成功！");
        result.setData(labelList);
        return result ;
    }

    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result searchLabelByPage(@RequestBody Label condition, @PathVariable Integer page, @PathVariable Integer size) {

        Page<Label> pageLabelData = labelService.searchLabelByPage(condition, page, size);
        PageResult pageData = new PageResult(pageLabelData.getTotalElements(), pageLabelData.getContent());
        result.setMessage("#search + 分页查询成功！@");
        result.setData(pageData);
        return result;
    }

    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        List<Label> labelList = labelService.findAll();
        result.setMessage("查询列表成功");
        result.setData(labelList);
        return result;
    }

    @RequestMapping(value = "/{labelId}",method = RequestMethod.GET)
    public Result findById(@PathVariable("labelId")String labelId){
        String header = request.getHeader(LabelConstant.request_header);
        log.info(" Authorization : " + header + "\n\n");
        Label label = labelService.findById(labelId);
        result.setMessage("查询单记录成功");
        result.setData(label);
        return result ;
    }

    @RequestMapping(value = "/demo",method = RequestMethod.GET)
    public Result demo(){
        int i = 1 / 0;
        return result ;
    }

}
